package com.github.ghsea.retry;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
 * @author guhai
 */
@Configuration
public class DefaultActionFactory implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    private static boolean springContext = false;

    public static AbstractRetryableAction getAction(Class actionClassName) throws IllegalAccessException, InstantiationException {
        if (springContext) {
            return (AbstractRetryableAction) applicationContext.getBean(actionClassName);
        } else {
            return (AbstractRetryableAction) actionClassName.newInstance();
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        DefaultActionFactory.applicationContext = applicationContext;
        springContext = true;
    }
}
